package com.abu.manager.service.impl;


import com.abu.manager.mapper.RoleMapper;
import com.abu.manager.pojo.Role;
import com.abu.manager.pojo.RolePermission;
import com.abu.manager.service.RolePermissionService;
import com.abu.manager.service.RoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author 阿布
 * @since 2021-02-03
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RolePermissionService rolePermissionService;

    @Override
    public Role queryRoleByManagerId(Long managerId) {
        return this.roleMapper.queryRoleName(managerId);
    }

    @Override
    @Transactional
    public Boolean removeRoleAndRolePermission(Long roleId) {
//        删除角色信息
        int count = this.roleMapper.deleteById(roleId);
//        删除角色权限信息
        boolean flag = this.rolePermissionService.remove(new QueryWrapper<RolePermission>().eq("role_id", roleId));
        return flag && count == 1;
    }

    @Override
    public String getRoleNameByManagerId(Long mgId) {
       return this.roleMapper.getRoleNameByManagerId(mgId);
    }
}
